package com.ruoyi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.domain.TestSubject;
import com.ruoyi.service.ITestSubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考试平台学科Controller
 */

//年级选择器在这个接口
@Api(tags = "学科管理接口")
@RestController
@RequestMapping("/exam/subject")
public class TestSubjectController extends BaseController
{
    @Autowired
    private ITestSubjectService testSubjectService;

    //TODO：根据年级查询信息
    /**
     * 查询考试平台学科列表
     */
    @ApiOperation("查询考试平台学科列表")
    @GetMapping("/list")
    public AjaxResult list(TestSubject testSubject)
    {
        startPage();
        List<TestSubject> list = testSubjectService.selectTestSubjectList(testSubject);
        return AjaxResult.success(list);
    }

    /**
     * 获取考试平台卷库详细信息
     */
    @ApiOperation("获取考试平台卷库详细信息")
    @GetMapping(value = "/{subjectId}")
    public AjaxResult getInfo(@PathVariable("subjectId") Long subjectId)
    {
        if(testSubjectService.selectTestSubjectByExamId(subjectId)!=null){
            return success(testSubjectService.selectTestSubjectByExamId(subjectId));
        }
        else {
            return AjaxResult.error("查无此试卷");
        }
    }

    /**
     * 新增考试平台学科
     */

    @ApiOperation("新增考试平台学科")
    @PostMapping
    AjaxResult add(@RequestBody TestSubject testSubject)
    {
        if(!testSubjectService.selectTestSubjectList(testSubject).isEmpty()){
            return AjaxResult.error("添加数据已重复");
        }else {
            //TODO:如果添加数据重复且删除标识为1，则修改为0
            return toAjax(testSubjectService.insertTestSubject(testSubject));
        }
    }

    /**
     * 修改考试平台学科
     */
    @ApiOperation("修改考试平台学科")
    @PutMapping
    public AjaxResult edit(@RequestBody TestSubject testSubject)
    {
        return toAjax(testSubjectService.updateTestSubject(testSubject));
    }

    /**
     * 逻辑删除考试平台学科
     */
    @ApiOperation("删除考试平台学科")
    @DeleteMapping
    public AjaxResult remove(@RequestBody TestSubject testSubject)
    {
        return toAjax(testSubjectService.updateTestSubject(testSubject));
    }

    /**
     * 获取年级下拉框
     */
    @ApiOperation("获取年级下拉框")
    @GetMapping("/grade")
    public AjaxResult getGrade()
    {
        return AjaxResult.success(testSubjectService.getGrade());
    }

    /**
     * 获取年级下拉框
     */
    @ApiOperation("获取学科下拉框")
    @GetMapping("/subject")
    public AjaxResult getSubject()
    {
        return AjaxResult.success(testSubjectService.getSubject());
    }
}
